import request from '@/utils/request'
import store from '@/store'
import { getCart, setCart } from '@/utils/storage'
export default {
  namespaced: true,
  state () {
    return {
      cart: getCart() || { cartList: [], cartNum: 0 }
    }
  },
  mutations: {
    setCartList (state, { newCartList, addNum }) {
      state.cart.cartList = newCartList
      state.cart.cartNum = addNum
      setCart(state.cart)
    },
    changeSel (state, productId) {
      const products = state.cart.cartList.find(item => item.productId === productId)
      if (products) {
        products.checked = !products.checked
      }
    },
    changeSelAll (state, checked) {
      state.cart.cartList.forEach(item => {
        item.checked = !checked
      })
    },
    changeCount (state, { productNum, productId }) {
      const products = state.cart.cartList.find(item => item.productId === productId)
      if (products) {
        products.quantity = productNum
      }
      setCart(state.cart)
    },
    deleteProduct (state, productId) {
      const newCartList = state.cart.cartList.filter(item => item.productId !== productId)
      state.cart.cartList = newCartList
      setCart(state.cart)
    }

  },
  actions: {
    async getCartAction (context) {
      const userId = store.state.user.userInfo.userId
      const res = await request.get(`/getCart?userId=${userId}`, {
        headers: {
          Authorization: store.state.user.userInfo.token
        }
      })
      let newCartList = res.data
      if (newCartList) {
        newCartList.forEach(item => {
          item.checked = true
        })
      } else {
        newCartList = []
      }
      const addNum = res.addNum
      context.commit('setCartList', { newCartList, addNum })
    },
    async changeCountAction (context, object) {
      const { productNum, productId } = object
      const quantity = productNum
      const userId = store.state.user.userInfo.userId
      if (productNum !== 0) {
        context.commit('changeCount', { productNum, productId })
        request.post('/updateCart', {
          userId,
          productId,
          quantity
        }, {
          headers: {
            Authorization: store.state.user.userInfo.token
          }
        })
      } else {
        context.commit('deleteProduct', productId)
        request.post('/deleteProduct', { userId, productId }, {
          headers: {
            Authorization: store.state.user.userInfo.token
          }
        })
      }
    },
    async changeCartListsAction (context, cart) {
      const userId = store.state.user.userInfo.userId
      const res = await request.post('/updateCartLists', { userId, cart }, {
        headers: {
          Authorization: store.state.user.userInfo.token
        }
      })
      console.log(res)
    },
    async clearCartAction (context) {
      const userId = store.state.user.userInfo.userId
      context.commit('setCartList', { newCartList: [], addNum: 0 })
      request.post('/clearCart', { userId }, {
        headers: {
          Authorization: store.state.user.userInfo.token
        }
      })
    }
  },
  getters: {
    selCartLists (state) {
      return state.cart.cartList.filter(item => item.checked)
    },
    selCartListsNoChecked (state) {
      return state.cart.cartList.filter(item => !item.checked)
    },
    selCount (state, getters) {
      return getters.selCartLists.reduce((sum, item) => Number(sum) + Number(item.quantity), 0)
    },
    selPrice (state, getters) {
      const price = getters.selCartLists.reduce((sum, item) => sum + item.price * item.quantity, 0)
      return price.toFixed(2)
    },
    isAllChecked (state) {
      return state.cart.cartList.every(item => item.checked)
    }

  }
}
